using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Appr;
using net_work.IService.Appr;

namespace net_work.Controllers.Appr;

[Route("api/[controller]")]
[ApiController]
public class FlowNodeController(ILogger<FlowNodeController> logger, IFlowNodeService flowNodeService) : ControllerBase
{
    /// <summary>
    /// 获取流程节点列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListFlowNodes([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取流程节点列表");
        var result = await flowNodeService.GetFlowNodesPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取流程节点信息
    /// </summary>
    /// <param name="flowNodeId">流程节点ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetFlowNodeById([FromQuery] long flowNodeId)
    {
        logger.LogInformation("获取流程节点信息");
        return await flowNodeService.SelectByIdAsync(flowNodeId) is { } flowNode
            ? AjaxResult.Success(flowNode)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 新增流程节点
    /// </summary>
    /// <param name="flowNodeAddRequest">新增流程节点实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> AddFlowNode([FromBody] FlowNodeAddRequest flowNodeAddRequest)
    {
        logger.LogInformation("新增流程节点");
        var flowNodeId = await flowNodeService.InsertFlowNode(flowNodeAddRequest);
        return flowNodeId > 0
            ? AjaxResult.Success(new { flowNodeId })
            : AjaxResult.Error();
    }

    /// <summary>
    /// 修改流程节点
    /// </summary>
    /// <param name="flowNodeUpdateRequest">修改流程节点实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> UpdateFlowNode([FromBody] FlowNodeUpdateRequest flowNodeUpdateRequest)
    {
        logger.LogInformation("修改流程节点");
        return await flowNodeService.UpdateFlowNode(flowNodeUpdateRequest) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除流程节点
    /// </summary>
    /// <param name="ids">流程节点ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> DeleteFlowNode([FromBody] List<long> ids)
    {
        logger.LogInformation("删除流程节点");
        return await flowNodeService.DeleteFlowNodeByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
}